<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <!-- 总结内容 -->
    <img src="./数据监测.png">
    <title>Document</title>
</head>

<body>
    <div id="root">
        <h1>你好，{{name}}</h1>
        <button @click="students.age++"> 年龄加一 </button>
        <h2>学生年龄{{student.age}}</h2>

        <button @click="add">点我添加性别</button>
        <button @click="student.sex='未知'">修改性别</button>
        <h2 v-if="student.sex">学生性别{{student.sex}}</h2>

        <button @click.once="addfriend">首位添加一个朋友</button>
        <h2>朋友们</h2>

        <button @click="update">修改首位朋友为张三</button>
        <ul>
            <li v-for="(f,index) in student.friends" :key="index">
                {{f.name}}--{{f.age}}
            </li>
        </ul>

        <button @click.once="addhobby">添加一个爱好</button>
        <button @click="updatehobby">修改首个爱好为开车</button>
        <h2>爱好</h2>
        <ul>
            <li v-for="(f,index) of student.hobbies" :key="index">
                {{f}}
            </li>
        </ul>
    </div>
</body>
<script>
    const vm = new Vue({
        el: "#root",
        data: {
            name: "SWPU",
            student: {
                name: "jerry",
                age: 18,
                friends: [
                    { name: "Tom", age: 35 },
                    { name: "Cherry", age: 80 },
                ],
                hobbies: ["抽烟", "喝酒", "烫头"],
            }
        },
        methods: {
            add() {
                // 仅能给data里面的对象加入属性，不允许直接操作data 或者 vm
                Vue.set(this.student, "sex", "男")
            },
            addfriend() {
                this.student.friends.unshift({ name: "jerry", age: "35" })
            },
            update() {
                this.student.friends[0].name = "张三"
            },
            addhobby() {
                this.student.hobbies.push("唱歌")
            },
            updatehobby(){
                // Vue.set(this.student.hobbies.splice(0,1,"开车"))

                // this.student.hobbies.splice(0,1,"开车")

                this.$set(this.student.hobbies,0,"开车")
            }

        }

    })


</script>

</html>